package p.ithorns.example.statemachine;

import com.alibaba.cola.statemachine.Action;
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.AlertFailCallback;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;

/**
 * ColaStateConfig
 *
 * @author Ht.L
 * @date 2023-09-09 18:01
 * @since 1.0.0
 */
public class ColaStateTest {

    public static void main(String[] args) {
        ColaStateTest test = new ColaStateTest();
        test.start();
    }

    public void start() {
        StateMachineBuilder<States, Events, StateObj> builder = StateMachineBuilderFactory.create();

        builder.setFailCallback(new AlertFailCallback<>());

        builder.externalTransition()
                .from(States.DRAFT).to(States.PUBLISH_TODO).on(Events.ONLINE)
                .when(context -> false)
                .perform(doAction());

        builder.externalTransition()
                .from(States.DRAFT).to(States.PUBLISH_DONE).on(Events.ONLINE)
                .when(context -> {
                    return true;
                })
                .perform(doAction());

        builder.externalTransition()
                .from(States.PUBLISH_TODO).to(States.PUBLISH_DONE).on(Events.PUBLISH)
                .perform(doAction());

        builder.externalTransition()
                .from(States.PUBLISH_DONE).to(States.DRAFT).on(Events.ROLLBACK)
                .perform(doAction());

        StateObj obj = new StateObj();
        obj.setId("1");
        obj.setState(States.DRAFT.name());

        StateMachine<States, Events, StateObj> machine = builder.build(obj.getState());
        // 输出初始状态
        System.out.println("初始状态：" + machine.getMachineId());

        States nextStates = machine.fireEvent(States.DRAFT, Events.ONLINE, obj);
        // 输出初始状态
        System.out.println("执行Events.ONLINE：" + nextStates);

        nextStates = machine.fireEvent(nextStates, Events.PUBLISH, obj);
        // 输出初始状态
        System.out.println("执行Events.PUBLISH：" + nextStates);

        nextStates = machine.fireEvent(nextStates, Events.ROLLBACK, obj);
        // 输出初始状态
        System.out.println("执行Events.ROLLBACK：" + nextStates);

//        System.out.println("showStateMachine：");
//        machine.showStateMachine();
//
//        System.out.println("generatePlantUML：" + machine.generatePlantUML());
    }


    // 执行动作方法
    private Action<States, Events, StateObj> doAction() {
        return new Action<States, Events, StateObj>() {
            @Override
            public void execute(States from, States to, Events event, StateObj obj) {
                prepare(from, to, event, obj);
                execute2(from, to, event, obj);
                clean(from, to, event, obj);
            }

            // 执行转换前的准备动作
            public void prepare(States from, States to, Events event, StateObj context) {
                System.out.println(String.format("准备转换: %s -> %s [event:%s] [message:%s]",
                        from, to, event, context.getState()));
            }

            // 执行转换动作
            public void execute2(States from, States to, Events event, StateObj context) {
                System.out.println(String.format("执行转换: %s -> %s [event:%s] [message:%s]",
                        from, to, event, context.getState()));
            }

            // 执行转换后的清理动作
            public void clean(States from, States to, Events event, StateObj context) {
                System.out.println(String.format("结束转换: %s -> %s [event:%s] [message:%s]",
                        from, to, event, context.getState()));
            }

        };
    }

}